/**
 * 加载状态
 *
 * @returns {
 *    startLoading: 开启加载
 *    closeLoading: 关闭加载
 *    loading:      加载状态
 *
 * }
 */
function useLoading() {
  //  加载状态的附带属性，用来说明加载状态结束时（isLoading变为false时），是以成功结束还是以失败结束
  const isSuccess = ref(false)
  const getIsSuccess = computed(() => isSuccess.value)

  const changeIsSuccess = (val: boolean) => {
    isSuccess.value = val
  }

  // 加载状态
  const loading = ref(false)
  const getLoading = computed(() => loading.value)
  const changeLoading = (val: boolean) => {
    loading.value = val
  }
  const startLoading = () => {
    changeLoading(true)
  }
  /**
   *
   * @param status :结束时候的状态，表示是在成功状态下结束的，还是在失败状态下结束的
   */
  const closeLoading = (status?: boolean) => {
    changeLoading(false)
    if (status !== undefined) {
      changeIsSuccess(status)
    }
  }

  return {
    startLoading,
    closeLoading,
    loading: getLoading,
    isSuccess: getIsSuccess
  }
}
export default useLoading
